package com.itheima.run;

import com.itheima.Utils.Utils;
import com.itheima.domain.Teacher;

import java.util.ArrayList;
import java.util.Scanner;

//教师管理类;
public class TeacherManager {
    public static void teacherManager(ArrayList<Teacher> list1) {
        while (true) {
            System.out.println("=====================================================");
            System.out.println("【教师信息管理】");
            System.out.println();
            System.out.println("1.添加教师  2.修改教师  3.查询教师  4.删除教师  5.返回");
            System.out.println("=====================================================");
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入菜单编号:");
            int i = sc.nextInt();
            switch (i) {
                case 1:
                    addteacher(list1);
                    break;
                case 2:
                    setteacher(list1);
                    break;
                case 3:
                    findteacher(list1);
                    break;
                case 4:
                    removeteacher(list1);
                    break;
                case 5:
                    return;
                default:
                    break;
            }
        }
    }

    public static void addteacher(ArrayList<Teacher> list1) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入教师姓名:");
        String name = sc.next();
        System.out.println("请输入教师性别:");
        String sex = sc.next();
        System.out.println("请输入教师生日:");
        String birthday = sc.next();
        int teaId = ++Utils.teaId;
        int age = Utils.birthdayToAge(birthday);
        Teacher teacher = new Teacher(teaId, name, sex, birthday, age);
        list1.add(teacher);
        System.out.println("信息添加成功.");
    }

    public static void findteacher(ArrayList<Teacher> list1) {
        if (list1.size() == 0) {
            System.out.println("没有你要查找的信息.");
        } else {
            Utils.printPersonList(list1);

        }
    }

    public static void removeteacher(ArrayList<Teacher> list1) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你要删除的教师编号:");
        int id = sc.nextInt();
        for (int i = 0; i < list1.size(); i++) {
            Teacher teacher = list1.get(i);
            int id1 = teacher.getId();
            if (id == id1) {
                list1.remove(i);
                System.out.println("删除成功");
                return;
            }
        }
        System.out.println("没有你要查找的信息.");
    }
    public static void setteacher(ArrayList<Teacher> list1){
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你要修改的教师编号:");
        int id = sc.nextInt();
        for (int i = 0; i < list1.size(); i++) {
            Teacher teacher = list1.get(i);
            int id1 = teacher.getId();
            if(id==id1){
                System.out.println("请输入新的教师姓名(不变请输入0):");
                String name = sc.next();
                System.out.println("请输入新的教师性别(不变请输入0):");
                String sex = sc.next();
                System.out.println("请输入新的教师生日(不变请输入0):");
                String birthday = sc.next();
                if(!name.equals("0")){
                    teacher.setName(name);
                }
                if(!sex.equals("0")){
                    teacher.setSex(sex);
                }
                if(!birthday.equals("0")){
                    teacher.setBirthday(birthday);
                    int age = Utils.birthdayToAge(birthday);
                    teacher.setAge(age);
                }
                System.out.println("信息修改成功.");
                return;
            }
        }
        System.out.println("没有查找到你要的编号.");
    }
}
